<!--
 * @Description: 添加弹窗组件
 * @Author: yue
 * @Date: 2022-09-17 11:42:40
 * @LastEditors: yue
 * @LastEditTime: 2022-09-20 10:56:21
-->

<!--
 * 自定义事件：
 * editDialog 编辑
 * 传值：
 * showDialog 弹层显示状态
 * subjectsList 科目数据
 * title 操作类型
 * directoryList 目录数据
-->

<template>
  <div class="container">
    <el-dialog
      :title="`${title}${name}`"
      :visible="showDialog"
      width="25%"
      left
      @close="btnCancel"
    >
      <el-form ref="form" :model="formDate" :rules="rules" label-width="80px">
        <el-form-item label="所属学科" prop="subjectID">
          <el-select v-model="formDate.subjectID" style="width: 300px">
            <el-option
              v-for="item in subjectsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="`${name}名称`" prop="directoryName">
          <el-input
            v-model="formDate[dataName]"
            style="width: 300px"
          ></el-input>
        </el-form-item>
      </el-form>

      <el-row type="flex" justify="end">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    // 使用方式（弹出层标题）
    title: {
      type: String,
      required: true
    },
    // 显示隐藏
    showDialog: {
      type: Boolean,
      required: true
    },
    // 学科列表（下拉菜单数据）
    subjectsList: {
      type: Array,
      required: true
    },
    // 目录列表（编辑用数据）
    directoryList: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      // 表单数据
      formDate: {
        subjectID: '',
        [this.dataName]: ''
      },
      // 表单验证
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ],
        directoryName: [
          {
            required: true,
            message: `请输入${this.$route.meta.title}名称`,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    // 页面名（弹出层标题）
    name() {
      return this.$route.meta.title
    },
    dataName() {
      return this.name === '标签' ? 'tagName' : 'directoryName'
    }
  },
  watch: {
    // 检测是否是编辑
    directoryList() {
      if (this.directoryList.id) {
        this.formDate = { ...this.directoryList }
      }
    }
  },
  methods: {
    // 关闭
    btnCancel() {
      this.$emit('update:show-dialog', false)
      this.$emit('update:title', '新增')
      this.$emit('update:directory-list', {})
      this.formDate = {
        subjectID: '',
        [this.dataName]: ''
      }
      this.$refs.form.clearValidate()
    },
    // 确认
    async btnOK() {
      try {
        await this.$refs.form.validate()
        this.$emit('editDialog', this.formDate)
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
